package com.baizhi.controller;

import com.baizhi.entity.TUser;
import com.baizhi.service.TUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/user")
public class TUserController {


    @Autowired
    private TUserService tUserService;


    @RequestMapping("/selectById/{id}/{username}")
    @ResponseBody
    public TUser selectByID(@PathVariable("id") Integer id, @PathVariable("username") String username) throws Exception {
        System.out.println(id);
        System.out.println(username);
        TUser tUser = tUserService.selectByID(id);
        System.out.println(tUser);
        return tUser;
    }

    //通过GET方式查询所有
    @GetMapping("/showAll")
    @ResponseBody
    public List<TUser> showAll() throws Exception {
        List<TUser> tUsers = tUserService.showAll();
        return tUsers;
    }

    //删除员工
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") Integer id, HttpServletResponse response) throws Exception {
        tUserService.delete(id);
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
    }

    //添加员工
    @PostMapping("/register")
    @ResponseBody
    public void register(@RequestBody TUser user) throws Exception {
        tUserService.register(user);
    }

    //修改员工
    @PutMapping("/update")
    @ResponseBody
    public void update(TUser user) throws Exception {
        System.out.println("++++++++++++++++++");
        tUserService.update(user);
    }
}
